package com.yhf.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhf.dto.ItemDto;
import com.yhf.mapper.ItemMapper;
import com.yhf.mapper.PMapper;
import com.yhf.mapper.SubitemMapper;
import com.yhf.model.Item;
import com.yhf.model.Subitem;
import com.yhf.service.ItemService;
import com.yhf.utils.PageResult;
import com.yhf.utils.QueryPageBean;
import com.yhf.utils.Result;
import com.yhf.vo.ItemSubitem;
import com.yhf.vo.SubitemEmg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    @Autowired
    ItemMapper itemMapper;
    @Autowired
    PMapper pMapper;
    @Autowired
    SubitemMapper subitemMapper;

    @Override
    public PageResult pageList(QueryPageBean queryPageBean) {
        Page<ItemDto> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<ItemDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        IPage<ItemDto> page1 = itemMapper.selectAll(page, queryWrapper);
        PageResult pageResult = new PageResult();
        pageResult.setRows(page1.getRecords());
        pageResult.setTotal(page1.getTotal());
        return pageResult;
    }

    @Override
    public Result findsubitem(String suid) {
        ItemSubitem itemsubitem = new ItemSubitem();
        QueryWrapper<Subitem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<Subitem> subitemes = subitemMapper.selectList(queryWrapper);
        SubitemEmg[] subitemEmgs = new SubitemEmg[subitemes.size()];
        for (int i = 0;i<subitemes.size();i++) {
            SubitemEmg subitemEmg = new SubitemEmg();
            subitemEmg.setLabel(subitemes.get(i).getSname());
            subitemEmg.setValue(subitemes.get(i).getSuid());
            subitemEmgs[i]=subitemEmg;
        }
        itemsubitem.setSubitemEmgs(subitemEmgs);
        if (suid != null) {
            itemsubitem.setChecked(suid);
        }
        Result result = new Result();
        result.setData(itemsubitem);
        result.setMessage("success");
        result.setFlag(true);
        return result;
    }

    @Override
    public Result Add(Item item) {
        int insert = itemMapper.insert(item);
        if (insert > 0) {
            return Result.success();
        }
        return Result.fail();

    }

    @Override
    public Result finditemById(String iid) {
        Result result = new Result();
        Item item = itemMapper.selectById(iid);
        result.setData(item);
        result.setMessage("success");
        result.setFlag(true);
        return result;
    }

    @Override
    public Result editByStage(Item item) {
        item.setUpdatetime(new Date());
        int i = itemMapper.updateById(item);
        if (i > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result deleteBySid(String iid) {
        Item item = new Item();
        item.setStatus(0);
        item.setIid(iid);
        int delete = itemMapper.updateById(item);
        if (delete >0) {
            return Result.success();
        }
        return Result.fail();
    }
}
